package com.mayikt.edu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mayikt.edu.base.BaseApiController;
import com.mayikt.edu.base.BaseResponse;
import com.mayikt.edu.domain.EduUser;
import com.mayikt.edu.dto.resp.EduUserRespDTO;
import com.mayikt.edu.service.EduUserService;
import com.mayikt.edu.utils.RedisUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangjin
 * @date 2023年10月30日 16:11
 */
@Slf4j
@RestController
public class UserController extends BaseApiController {
    @Autowired
    public EduUserService eduUserService;

    /**
     * 传递用户令牌
     *
     * @return
     */
    @ApiOperation(value = "传递用户令牌", notes = "传递用户令牌")
    @GetMapping("/getByTokenUserInfo")
    public BaseResponse getByTokenUserInfo(@RequestHeader String token) {
        //1验证参数
        if (StringUtils.isBlank(token)) {
            log.error("[token is null]");
            return setResultError();
        }
        //2. 根据token查询用户信息
        String redisValue = RedisUtils.getString(token);
        if (StringUtils.isBlank(redisValue)) {
            return setResultError("token error");
        }
        Integer.valueOf(redisValue);
        EduUser eduUser = eduUserService.queryById(Integer.valueOf(redisValue));
        //do 转dto
        EduUserRespDTO eduUserRespDTO = BeanUtil.toBean(eduUser, EduUserRespDTO.class);


        return setResultSuccessData(eduUserRespDTO);
    }
}
